/*
 * @Author: Superficial
 * @Date: 2019-11-15 22:49:46
 * @LastEditTime: 2021-02-22 12:23:23
 * @Description: 工具函数
 */
/**
 * @description: 时间日期字符串
 * @param {time} 时间日期字符串
 * @return: 格式化后的时间字符串
 */
 let dateFormat = time => {
    const date = new Date(time);
    const Time = {
      YY: date.getFullYear(),
      MM: date.getMonth() + 1,
      DD: date.getDate(),
      hh: date.getHours(),
      mm: date.getMinutes(),
      ss: date.getSeconds()
    };
    for (let key in Time) {
      Time[key] = Time[key] < 10 ? `0${Time[key]}` : `${Time[key]}`;
    }
    return `${Time.YY}-${Time.MM}-${Time.DD} ${Time.hh}:${Time.mm}:${Time.ss}`;
  };
  
  const timeAgo = (_time) => {
    const time = new Date(_time)
    const between = Date.now() / 1000 - (Number(time) / 1000)
    const hourS = 3600
    const dayS = hourS * 24
    const weekS = dayS * 7
    const monthS = dayS * 30
    const yearS = monthS * 12
  
    const pluralize = (time, label) => {
      return `${time} ${label}前`
    }
  
    if (between < hourS) {
      return ~~(between / 60) === 0
        ? "刚刚"
        : pluralize(~~(between / 60), "分钟")
    }
    if (between < dayS) {
      return pluralize(~~(between / hourS), "小时")
    }
    if (between < weekS) {
      return pluralize(~~(between / dayS), "天")
    }
    if (between < monthS) {
      return pluralize(~~(between / weekS), "周")
    }
    if (between < yearS) {
      return pluralize(~~(between / monthS), "个月")
    }
    return pluralize(~~(between / yearS), "年")
  }
  
  export { dateFormat, timeAgo };
  